Skip to content

fix SQLAlchemy compatibility issues on macOS#7724

Open
hibiki233i wants to merge 5 commits intoAstrBotDevs:masterfrom
hibiki233i:hibiki233i/Fix-SQLAlchemy-compatibility-on-macOS
Open

fix SQLAlchemy compatibility issues on macOS#7724
hibiki233i wants to merge 5 commits intoAstrBotDevs:masterfrom
hibiki233i:hibiki233i/Fix-SQLAlchemy-compatibility-on-macOS

Conversation

@hibiki233i
Copy link
Copy Markdown

@hibiki233i hibiki233i commented Apr 22, 2026

fix #7722

Modifications / 改动点

packaged desktop runtime 把 app 内置依赖和 ~/.astrbot/data/site-packages 里的用户依赖混到了同一个 Python 进程里。
main.py 调整了启动时的 sys.path 处理逻辑。现在在 packaged desktop runtime 下,不再把 ~/.astrbot/data/site-packages 直接追加进全局导入路径。核心后端优先固定使用 app 自带依赖,避免和用户目录里的 SQLAlchemy、SQLModel 等包混装。
astrbot/core/db/init.py 、astrbot/core/knowledge_base/kb_db_sqlite.py 、astrbot/core/db/vec_db/faiss_impl/document_storage.py 把 sqlite+aiosqlite 的异步引擎都改成了显式 NullPool。不再依赖 SQLAlchemy 对 SQLite async 的默认连接池选择,减少 desktop/launcher 打包环境里因为默认池事件支持差异导致的兼容性问题。
documents 表初始化从 metadata.create_all() 改成了显式 SQL CREATE TABLE IF NOT EXISTS。知识库向量库文档表的建表不再依赖 ORM metadata 反射路径,从而绕开 packaged runtime 下最容易被双份 SQLAlchemy 污染的 has_table()/create_all() 调用链。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

录屏2026-04-22 下午2 42 19

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • [√] 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • [√] 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • [√] 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Ensure packaged desktop runtime uses bundled dependencies reliably and avoids SQLite async engine issues in bundled environments.

Bug Fixes:

  • Prevent packaged desktop runtime from mixing bundled core Python dependencies with user-installed packages in ~/.astrbot/data/site-packages, avoiding conflicts with libraries like SQLAlchemy and SQLModel.
  • Avoid reliance on SQLAlchemy's default async SQLite connection pooling to prevent compatibility issues in packaged desktop/launcher environments.

Enhancements:

  • Create the FAISS document storage SQLite table via explicit SQL instead of ORM metadata reflection for more stable behavior in packaged runtimes.

Summary by Sourcery

Resolve packaged desktop runtime compatibility issues with SQLAlchemy and SQLite on macOS by isolating bundled dependencies and hardening SQLite async usage.

Bug Fixes:

  • Prevent packaged desktop runtime from adding ~/.astrbot/data/site-packages to the global import path so bundled core libraries do not conflict with user-installed packages.
  • Ensure async SQLite engines consistently use a NullPool to avoid dialect- and environment-dependent default pooling behavior that breaks in packaged runtimes.

Enhancements:

  • Initialize the FAISS document storage SQLite table using an explicit CREATE TABLE IF NOT EXISTS statement instead of ORM metadata reflection for more robust behavior in packaged environments.

@auto-assign auto-assign Bot requested review from Fridemn and advent259141 April 22, 2026 07:00
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Apr 22, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The raw SQL in _ensure_documents_table hardcodes the documents schema independently of BaseDocModel, which risks divergence if the model changes later; consider centralizing the schema definition or at least adding a clear comment tying this DDL to the model so future changes stay in sync.
  • In _ensure_documents_table (and the related FTS helpers), the executor argument is actually an async connection; renaming it (e.g., to conn) and adding a concrete type hint (such as AsyncConnection) would make its expected interface clearer and reduce misuse.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The raw SQL in `_ensure_documents_table` hardcodes the `documents` schema independently of `BaseDocModel`, which risks divergence if the model changes later; consider centralizing the schema definition or at least adding a clear comment tying this DDL to the model so future changes stay in sync.
- In `_ensure_documents_table` (and the related FTS helpers), the `executor` argument is actually an async connection; renaming it (e.g., to `conn`) and adding a concrete type hint (such as `AsyncConnection`) would make its expected interface clearer and reduce misuse.

## Individual Comments

### Comment 1
<location path="astrbot/core/knowledge_base/kb_db_sqlite.py" line_range="43-47" />
<code_context>
                 self.DATABASE_URL,
                 echo=False,
                 future=True,
+                poolclass=NullPool,
             )
             self.async_session_maker = sessionmaker(
</code_context>
<issue_to_address>
**suggestion:** Remove or adjust `pool_pre_ping`/`pool_recycle` when using `NullPool` for the SQLite engine.

With `poolclass=NullPool`, connections are created and disposed per use, so `pool_pre_ping` and `pool_recycle` are effectively no-ops and may mislead future readers about connection liveness guarantees. Consider removing these arguments here, or switching to an actual pool (e.g., `StaticPool`/`SingletonThreadPool`) if those behaviors are required for this database.

```suggestion
            echo=False,
            poolclass=NullPool,
        )
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/knowledge_base/kb_db_sqlite.py
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves stability for packaged desktop runtimes by switching to NullPool for SQLite connections and using raw SQL for table creation in the vector database. It also prevents library mixing in main.py for packaged environments. Feedback includes a suggestion to add a UNIQUE constraint to the doc_id column to prevent potential query errors and to remove redundant pooling configurations that are no longer effective with NullPool.

Comment thread astrbot/core/db/vec_db/faiss_impl/document_storage.py Outdated
Comment thread astrbot/core/knowledge_base/kb_db_sqlite.py
hibiki233i and others added 2 commits April 22, 2026 15:29
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@hibiki233i hibiki233i changed the title fix issues#7722 fix SQLAlchemy compatibility issues on macOS Apr 22, 2026
@Soulter Soulter requested a review from zouyonghe April 23, 2026 03:47
@zouyonghe
Copy link
Copy Markdown
Member

@sourcery-ai review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The new _ensure_documents_table uses a hardcoded CREATE TABLE schema; consider centralizing the table definition (or deriving it from BaseDocModel) so that field/constraint changes in the model cannot silently drift from the raw SQL.
  • The is_sqlite = "sqlite" in self.DATABASE_URL check in astrbot/core/db/__init__.py could mis-detect non-SQLite URLs containing the substring; using SQLAlchemy’s URL parsing (make_url) or a stricter prefix/scheme check would make the engine configuration more robust.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `_ensure_documents_table` uses a hardcoded `CREATE TABLE` schema; consider centralizing the table definition (or deriving it from `BaseDocModel`) so that field/constraint changes in the model cannot silently drift from the raw SQL.
- The `is_sqlite = "sqlite" in self.DATABASE_URL` check in `astrbot/core/db/__init__.py` could mis-detect non-SQLite URLs containing the substring; using SQLAlchemy’s URL parsing (`make_url`) or a stricter prefix/scheme check would make the engine configuration more robust.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]macos上主程序使用launch\desktop运行时SQLAlchemy存在兼容性问题

2 participants